﻿Imports BSE.Windows.Forms

Public Class MainForm

    Private _last大分类 As String = ""
    Private _last小分类 As String = ""

    Private lstViewS As New List(Of ListView)
    Private _lstS As New List(Of SoftWareEdit)
    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Using frm As New TypeList
            frm.ShowDialog()
            If frm.是否更新 Then
                Me.InitBigType()
            End If
        End Using
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Using frm As New BigTypeList
            frm.ShowDialog()
            If frm.是否更新 Then
                Me.InitBigType()
            End If
        End Using
    End Sub

    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Using frm As New SoftWareList("")
            frm.ShowDialog()
            If frm.是否更新 Then
                Me.InitBigType()
            End If
        End Using
    End Sub

    Private Sub ToolStripMenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Using frm As New SettingForm
            If frm.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                RefreshForm1()
            End If
        End Using
    End Sub

    Public Sub RefreshForm1()

        Me.WysSkinUC1.是否使用皮肤功能 = (MyConfig.使用皮肤 = "1")
        Me.WysSkinUC1.ChangeSkin(MyConfig.皮肤名称, Me.WysSkinUC1.是否使用皮肤功能)
        Me.Text = MyConfig.标题
        'InitMenu()
        InitBigType()
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '检查文件夹是否存在
        If Not System.IO.Directory.Exists(Util.GetCurrentFolderDb) Then
            System.IO.Directory.CreateDirectory(Util.GetCurrentFolderDb)
        End If

        If Not System.IO.Directory.Exists(Util.GetCurrentFolderIcon) Then
            System.IO.Directory.CreateDirectory(Util.GetCurrentFolderIcon)
        End If

        If Not System.IO.Directory.Exists(Util.GetCurrentFolderSoftWare) Then
            System.IO.Directory.CreateDirectory(Util.GetCurrentFolderSoftWare)
        End If

        InitMenu()

        InitBigType()

        InitStatus()

        Me.Width = CInt(MyConfig.窗体宽度)
        Me.Height = CInt(MyConfig.窗体高度)

        Me.sc1.SplitterDistance = CInt(MyConfig.左边面板宽度())
        Util.CenterForm(Me)

        Me.Text = MyConfig.标题

    End Sub

    Private _firstInit As Boolean = False
    Private Sub InitLeft(ByRef xp As XPanderPanel, ByVal bigTypeId As String)
        Dim lstView As New ListView
        lstView.HideSelection = False
        lstView.MultiSelect = False
        lstView.View = View.LargeIcon
        lstView.Dock = DockStyle.Fill
        lstView.Items.Clear()

        Dim dt As DataTable = Util.GetSoftWareType(bigTypeId)
        Dim al As New ImageList

        Dim images As New ImageList
        images.ImageSize = New Size(32, 40)

        lstView.SmallImageList = images
        lstView.LargeImageList = images
        
        For index As Integer = 0 To dt.Rows.Count - 1
            images.Images.Add(index.ToString, CType(dt.Rows(index)("image"), Image))
            Dim lvm As ListViewItem = lstView.Items.Add(CStr(dt.Rows(index)("name")), index.ToString)
            lvm.SubItems.Add(CStr(dt.Rows(index)("id")))
        Next

        If _firstInit = False AndAlso lstView.Items.Count > 0 AndAlso _lastSelectindex < lstView.Items.Count - 1 Then
            lstView.Items(_lastSelectindex).Selected = True
            _firstInit = True
        End If
        xp.Controls.Add(lstView)
        lstViewS.Add(lstView)
        AddHandler lstView.Click, AddressOf lstView_SelectedIndexChanged
        AddHandler lstView.DoubleClick, AddressOf lstView_DoubleClick
    End Sub

    Public Sub CaptionClick(ByVal sender As Object, ByVal e As BSE.Windows.Forms.XPanderPanelClickEventArgs)
        If e.Expand = True Then
            Dim xp As XPanderPanel = sender
            If xp IsNot Nothing Then
                Me._last大分类 = xp.Tag
            End If
        End If
    End Sub

    Private Sub InitBigType()
        _firstInit = False
        lstViewS.Clear()

        sc1.Panel2.Controls.Clear()
        _lstS.Clear()

        bigType.XPanderPanels.Clear()
        Dim dt As DataTable = Util.GetSoftWareBigType
        For index As Integer = 0 To dt.Rows.Count - 1
            Dim xpPanel As New BSE.Windows.Forms.XPanderPanel
            xpPanel.Image = CType(dt.Rows(index)("image"), Image)
            xpPanel.Text = CStr(dt.Rows(index)("name"))
            InitLeft(xpPanel, CStr(dt.Rows(index)("id")))
            xpPanel.Tag = CStr(dt.Rows(index)("id"))
            AddHandler xpPanel.CaptionClick, AddressOf CaptionClick
            bigType.XPanderPanels.Add(xpPanel)
        Next

        If bigType.XPanderPanels.Count > 0 Then
            bigType.XPanderPanels(0).Expand = True
            _last大分类 = bigType.XPanderPanels(0).Tag
        End If

        If lstViewS.Count > 0 Then
            If lstViewS.Item(0).Items.Count > 0 Then
                lstViewS.Item(0).Items(0).Selected = True
                lstView_SelectedIndexChanged(lstViewS.Item(0), Nothing)
            End If
        End If

    End Sub

    Private Sub InitMenu()
        Dim menuitemtype As New ToolStripMenuItem("设置(&T)")
        menuitemtype.DropDownItems.Add("软件大分类", Nothing, AddressOf ToolStripMenuItem2_Click)
        menuitemtype.DropDownItems.Add("软件小分类", Nothing, AddressOf ToolStripMenuItem1_Click)
        menuitemtype.DropDownItems.Add("软件列表", Nothing, AddressOf ToolStripMenuItem3_Click)
        Me.MenuStrip1.Items.Insert(0, CType(menuitemtype, ToolStripMenuItem))
        sc1.Panel2.ContextMenuStrip = Me.ContextMenuStripAdd
        menuitemtype.DropDownItems.Add("配置", Nothing, AddressOf ToolStripMenuItem5_Click)
    End Sub

    Private Sub InitStatus()
        StatusStrip1.Items.Clear()
        StatusStrip1.Visible = False
    End Sub

    Private Sub RefreshRightForm()
        Dim startTop As Integer = 3
        Dim startLeft As Integer = 3

        Dim ty As New XMLSoftWare
        Dim cws As List(Of CSoftWare) = ty.GetByTypeId(Me._last小分类)
        sc1.Panel2.Controls.Clear()
        _lstS.Clear()

        For index As Integer = 0 To cws.Count - 1

            If MyConfig.模式 = "0" Then
                Dim sy As SoftWare = New SoftWare
                sy.软件 = cws(index)
                sy.Left = startLeft
                sy.Top = startTop + index * (50 + 3)
                sy.Height = 50
                sy.Width = sc1.Panel2.Width - 6
                sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                sc1.Panel2.Controls.Add(sy)
            Else
                Dim sy As SoftWareEdit = New SoftWareEdit
                AddHandler sy.EditSoftWare, AddressOf 修改Click
                AddHandler sy.DelSoftWare, AddressOf 删除Click
                sy.软件 = cws(index)
                sy.Left = startLeft
                sy.Top = startTop + index * (50 + 3)
                sy.Height = 50
                sy.Width = sc1.Panel2.Width - 6
                sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                sc1.Panel2.Controls.Add(sy)
            End If
        Next
    End Sub

    Private Sub InitSoftWare()

        Dim startTop As Integer = 3
        Dim startLeft As Integer = 3

        Dim ty As New XMLSoftWare
        Dim cws As List(Of CSoftWare) = ty.GetByTypeId(Me._last小分类)
        sc1.Panel2.Controls.Clear()
        _lstS.Clear()

        For index As Integer = 0 To cws.Count - 1
            If MyConfig.模式 = "0" Then
                Dim sy As SoftWare = New SoftWare
                sy.软件 = cws(index)
                sy.Left = startLeft
                sy.Top = startTop + index * (50 + 3)
                sy.Height = 50
                sy.Width = sc1.Panel2.Width - 6
                sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                sc1.Panel2.Controls.Add(sy)
            Else
                Dim sy As SoftWareEdit = New SoftWareEdit
                AddHandler sy.EditSoftWare, AddressOf 修改Click
                AddHandler sy.DelSoftWare, AddressOf 删除Click
                sy.软件 = cws(index)
                sy.Left = startLeft
                sy.Top = startTop + index * (50 + 3)
                sy.Height = 50
                sy.Width = sc1.Panel2.Width - 6
                sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                sc1.Panel2.Controls.Add(sy)
            End If
        Next
    End Sub

    Private Sub lstView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim startTop As Integer = 3
        Dim startLeft As Integer = 3

        Dim lstView As ListView = sender
        If lstView Is Nothing Then
            Return
        End If

        If lstView.Items.Count = 0 Then
            Return
        End If

        If lstView.SelectedItems.Count = 0 Then
            Return
        End If

        'Me.SuspendLayout()
        'sc1.Panel2.AutoScroll = True

        Dim lvm As ListViewItem = lstView.SelectedItems(0)
        Dim typeid As String = lvm.SubItems(1).Text

        Me._last小分类 = typeid

        Dim ty As New XMLSoftWare
        Dim cws As List(Of CSoftWare) = ty.GetByTypeId(typeid)
        sc1.Panel2.Controls.Clear()
        _lstS.Clear()

        For index As Integer = 0 To cws.Count - 1

            If MyConfig.模式 = "0" Then
                Dim sy As SoftWare = New SoftWare
                sy.软件 = cws(index)
                sy.Left = startLeft
                sy.Top = startTop + index * (50 + 3)
                sy.Height = 50
                sy.Width = sc1.Panel2.Width - 6
                sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                sc1.Panel2.Controls.Add(sy)
            Else
                Dim sy As SoftWareEdit = New SoftWareEdit
                AddHandler sy.EditSoftWare, AddressOf 修改Click
                AddHandler sy.DelSoftWare, AddressOf 删除Click
                sy.软件 = cws(index)
                sy.Left = startLeft
                sy.Top = startTop + index * (50 + 3)
                sy.Height = 50
                sy.Width = sc1.Panel2.Width - 6
                sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                sc1.Panel2.Controls.Add(sy)
            End If
        Next

    End Sub

    Private Sub lstView_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim lstView As ListView = sender
        'If lstView Is Nothing Then
        '    Return
        'End If

        'If lstView.Items.Count = 0 Then
        '    Return
        'End If

        'If lstView.SelectedItems.Count = 0 Then
        '    Return
        'End If

        'Dim lvm As ListViewItem = lstView.SelectedItems(0)
        'Dim typeid As String = lvm.SubItems(1).Text

        'Dim ty As New XMLSoftWare
        'Dim cws As List(Of CSoftWare) = ty.GetByTypeId(typeid)

        'Using frm As New SoftWareList(typeid)
        '    'SetLastIndex()
        '    frm.ShowDialog()
        '    If frm.是否更新 Then
        '        InitBigType()
        '    End If
        'End Using

    End Sub

    Private _lastSelectindex As Integer = 0
    Private _currentBigType As String = ""

    Private ReadOnly Property CurrentBigType() As String
        Get
            For Each xp As XPanderPanel In bigType.XPanderPanels
                If xp.Expand = True Then
                    Return CStr(xp.Tag)
                End If
            Next
            Return ""
        End Get
    End Property

    Private Sub sc1_Panel2_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles sc1.Panel2.Scroll
        For index As Integer = 0 To sc1.Panel2.Controls.Count - 1
            sc1.Panel2.Controls(index).Width = sc1.Panel2.Width - 6 - 17
        Next
    End Sub

    Private Sub 修改Click(ByVal typeid As String)

        Dim ty As New XMLSoftWare
        Dim rj As CSoftWare = ty.GetById(typeid)
        Dim lsttype As New List(Of String)

        rj.TypeId = lsttype
        Using frm As New SoftWareAdd(rj)
            frm.Text = "修改软件"
            frm.ShowDialog()
            If frm.更新 = True Then
                InitSoftWare()
            End If
        End Using
    End Sub

    Private Sub 删除Click(ByVal typeid As String)

        If wys.util.Common.MessageBase.ShowMessage(Me.Handle, "是否删除次软件？", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
            Dim ty As New XMLSoftWare
            ty.Delete(typeid)
            InitSoftWare()
        End If
    End Sub

    Private Sub 添加软件ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 添加软件ToolStripMenuItem.Click
        Dim ty As New CSoftWare
        Dim tys As New XMLSoftWare
        ty.Id = tys.GetMaxid
        Dim lsttype As New List(Of String)
        lsttype.Add(Me._last小分类)
        ty.TypeId = lsttype

        Using frm As New SoftWareAdd(ty)
            AddHandler frm.Updateparent, AddressOf RefreshRightForm
            frm.Text = "添加软件"
            frm.ShowDialog()
            If frm.更新 = True Then
                'InitSoftWare()
                '_更新 = True
            End If
        End Using
    End Sub

    Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            MyConfig.窗体高度 = CStr(Me.Height)
            MyConfig.窗体宽度 = CStr(Me.Width)
            MyConfig.左边面板宽度 = CStr(sc1.SplitterDistance)
        Catch ex As Exception
            'wys.util.Common.MessageBase.ShowMessage(ex.ToString)
        End Try
      
    End Sub

    Public Sub New()
        MyConfig.DefaultContent = Util.GetAppConfigTemple

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.WysSkinUC1.SkinName = MyConfig.皮肤名称
        Me.WysSkinUC1.是否使用皮肤功能 = (MyConfig.使用皮肤 = "1")
    End Sub

   
End Class
